package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.application.Cache;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.HomeListItem;
import com.sctvcloud.dongqu.beans.LinkItem;
import com.sctvcloud.dongqu.http.AbsListNetCallback;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.adapter.CivilServiceAdapter;
import com.sctvcloud.dongqu.ui.adapter.GovernmentCenterAdapter;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;
import com.sctvcloud.dongqu.utils.ListTypeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 政务大厅
 */
public class GovernmentCenterActivity extends BaseActivity implements CanRefreshLayout.OnRefreshListener {

    @BindView(R.id.common_frg_refreshlayout)
    protected CanRefreshLayout mRefreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;
    private GovernmentCenterAdapter mAdapter;
    private boolean mIsLoading = false;
    public static final String GOV_CENTER_URL = "GOV_CENTER_URL";
    private String url;
    private List<LinkItem> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("政务大厅").setBackVisible(true);
        url = getIntent().getStringExtra(GOV_CENTER_URL);
        initView();

    }


    @Override
    protected void initInject() {
        setContentView(R.layout.activity_government_hall);
        ButterKnife.bind(this);
    }


    private void initView() {
        mRefreshLayout.setOnRefreshListener(this);
//        mRefreshLayout.setOnLoadMoreListener(this);
        GridLayoutManager mgr = new GridLayoutManager(this, 4);
        mRecyclerView.setLayoutManager(mgr);
//        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
//        mRecyclerView.addItemDecoration(new RBaseItemDecoration(1));
        mAdapter = new GovernmentCenterAdapter(this, null).setItemInternalClick(itemClick);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        dataList.clear();
        mIsLoading = true;
        getLinkArray(url);
    }


    /**
     * 获取 政务大厅
     *
     * @param url
     * @return
     */
    private void getLinkArray(String url) {

        NetUtils.getNetAdapter().getLinkArray(getOwnerName(), url, new AbsListNetCallback<LinkItem>(ListTypeUtils.LinkItem(), 0) {//暂时使用calledIndex来保存mode
            @Override
            public void onSuc(List<LinkItem> list) {
                if (ListUtils.isListValued(list)) {
                    dataList.addAll(list);
                }
                mAdapter.setData(dataList);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });
    }


    private synchronized void onNetFinish() {
        mRefreshLayout.refreshComplete();
        mIsLoading = false;
    }

    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            if (ListUtils.isListValued(dataList) && dataList.size() > position) {
                Intent intent = new Intent(GovernmentCenterActivity.this, H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.EX_TITLE, dataList.get(position).getName());
                intent.putExtra(H5NormalActivity.EX_HTML, dataList.get(position).getLink());
                startActivity(intent);
            }
        }
    };

}
